package com.jiuyin.dianjing.ui.activity.team;

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.api.base.DefaultApiResult;
import com.jiuyin.dianjing.api.data.TokenJson;
import com.jiuyin.dianjing.api.enums.ApiEnum;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.event.RefreshEvent;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_COMMENT;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_GAMENAME;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_INTRO;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_STATUS;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TEAMID;

public class JoinTeam2Activity extends BaseActivity {
    @BindView(R.id.tv_name_value)
    TextView tvUsername;
    @BindView(R.id.tv_id_value)
    EditText etId;
    @BindView(R.id.tv_duanwei_value)
    EditText etDuanwei;
    @BindView(R.id.tv_remark_value)
    EditText etRemark;
    @BindView(R.id.bt_submit)
    Button btSubmit;

    String teamId;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_join_team2;
    }

    @Override
    protected void initData() {
        teamId = getIntent().getStringExtra(KEY_TEAMID);
    }

    @Override
    protected void initView() {
        setTitle("申请加入");
        tvUsername.setText(HelperApplication.getNickName());

        btSubmit.setOnClickListener(v -> {
            JSONObject data;
            try {
                data = TokenJson.newInstance()
                        .put(KEY_TEAMID, teamId)
                        .put(KEY_GAMENAME, etId.getText().toString())
                        .put(KEY_INTRO, etDuanwei.getText().toString())
                        .put(KEY_COMMENT, etRemark.getText().toString());
            } catch (JSONException e) {
                e.printStackTrace();
                return;
            }
            ApiResultCallback apiRequestCallback = new DefaultApiResult() {
                @Override
                public void onSuccess(JsonObject data) {
                    String status = data.get(KEY_STATUS).getAsString();
                    if (status.equals("0")) {
                        ToastUtil.showShort("申请已发送");
                        EventBus.getDefault().post(new RefreshEvent(TeamDetailNewActivity.class.getSimpleName()));
                        finish();
                    }
                }
            };
            ServerApi.post(ApiEnum.APPLY_FOR_TEAM, data, null, progress, null, apiRequestCallback);
        });
    }
}
